<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class UserLisenceRepository {

    /**
     * 获取资质审核列表
     * @param  $param
     * @param  $page
     * @param  $perPage
     * @param  $type
     * @return bool
     */
    public function userLisenceList($param, $page = 1, $perPage = 20,$type=2) {
        $list = app('model_user_lisence')
            ->with('lisence:lisence_id,lisence_name')
            ->with('user_data:user_id,user_code,nickname')
//            ->where('status', '<>', 4)
            ->whereBetween('add_time', [$param['add_start_time'], $param['add_end_time']])
            ->whereBetween('update_time', [$param['save_start_time'], $param['save_end_time']]);
        if ($type==1){
            $list=$list->where('status', '<>', 4);
            if (isset($param['status'])) {
                $list = $list->where('status', $param['status']);
            }
        }
        if ($type==2){
            $list=$list->where('status',1);
        }
        if (isset($param['lisence_id'])) {
            $list = $list->where('lisence_id', $param['lisence_id']);
        }
        if (isset($param['is_open'])) {
            $list = $list->where('is_open', $param['is_open']);
        }
        if (isset($param['is_verify'])) {
            $list = $list->where('is_verify', $param['is_verify']);
        }
        if (isset($param['user_code'])) {
            $user_code = $param['user_code'];
            $list = $list->whereHas('user_data', function ($query) use ($user_code) {
                $query->where('user_code', 'like', "%{$user_code}%");
            });
        }
        $list = $list->orderBy('add_time', 'desc')->paginate($perPage, ['*'], '', $page);
        return $list;
    }


    /**
     * 删除资质数据
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function delUserLisence($id) {
        $data = [
            'status' => 4,
            'update_time' => time(),
        ];
        app('model_user_lisence')->whereIn('lisence_id', $id)->whereIn('status', [1, 2])->update($data);
    }


    /**
     * 资质审核失败操作
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function examineFail($id) {
        $data = [
            'status' => 3,
            'update_time' => time(),
        ];
        $suspend = app('model_user_lisence')->where('last_apply_id', $id)->update($data);
        if ($suspend) return $suspend;
        throw new BasicException(93128);
    }

    /**
     * 查询数据是否存
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function isHaveData($id) {
        $isHaveData = app('model_user_lisence')->where('last_apply_id', $id)->first();
        return $isHaveData;
    }

    /**
     * 资质审核成功操作
     * @param  $id
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public function examineSuccess($id, $param) {
        $save = app('model_user_lisence')->where('last_apply_id', $id)->update($param);
        if ($save) return $save;
        throw new BasicException(93130);
    }


    /**
     * 修改unique_tag
     * @param  $id
     * @param  $unique_tag
     * @return bool
     * @throws BasicException
     */
    public function saveUniqueTag($id, $unique_tag) {
        app('model_user_lisence')->where('last_apply_id', $id)->update(['unique_tag' => $unique_tag]);
    }

    /**
     * 编辑最大单价
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public function saveMaxPrice($param) {
        app('model_user_lisence')->where('id', $param['id'])->update(['max_price' => $param['price']]);
    }
    /**
     * 编辑技术认证
     * @param  $param
     * @return bool
     * @throws BasicException
     */
    public function saveVerify($param) {
        app('model_user_lisence')->where('id', $param['id'])->update(['is_verify' => $param['is_verify']]);
    }

    /**
     * 编辑技术认证
     * @param  $last_apply_id
     * @return bool
     * @throws BasicException
     */
    public function uniqueTag($last_apply_id) {
        $uniqueTag = app('model_user_lisence')->where('last_apply_id', $last_apply_id)->value('unique_tag');
        return $uniqueTag;
    }

}


?>